import product1 from '@img/product/product-1.jpg'

/**
 * 将后台的goodDetail数据转换成SimpleGood中的数据
 * @param detail {Object}
 * @return {{Object}}
 */
export const convertDetailToSimpleGoodItem = (detail) => {
  const item = {}
  item.id = detail.id
  item.title = detail.name
  item.hot = detail.score
  if (detail['subGoodList'] && typeof detail['subGoodList'] === 'object') {
    detail['subGoodList'].forEach((sub, index) => {
      if (index === 0) {
        item.price = sub.price
      }
      if (item.price > sub.price) {
        item.price = sub.price
      }
    })
  }
  const pictures = detail['goodPictureList']
  if (pictures && typeof pictures === 'object') {
    if (pictures.length === 0) {
      item.image1 = product1
      item.image2 = product1
    }
    if (pictures.length === 1) {
      item.image1 = pictures[0].image
      item.image2 = pictures[0].image
    }
    if (pictures.length > 1) {
      item.image1 = pictures[0].image
      item.image2 = pictures[1].image
    }
  }
  return item
}

/**
 * 将后台的goodDetail数据转换成SimpleGood中的数据
 * @param detail {Array<Object>} 数组对象
 * @return {{Object}}
 */
export const convertDetailToSimpleGoodList = (detail) => {
  if (detail && typeof detail === 'object') {
    const list = []
    detail.forEach(item => {
      list.push(convertDetailToSimpleGoodItem(item))
    })
    return list
  }
  return []
}

/**
 * 将后台的goodDetail数据转换成用户order商品也中的数据
 * @param detail {Object}
 * @param subGoodId {String | Number} 子商品id
 * @return {{Object}}
 */
export const convertDetailToOrderItem = (detail, subGoodId) => {
  const item = {}
  Object.assign(item, detail)
  item.id = detail.id
  item.title = detail.name
  item.hot = detail.score
  if (detail['subGoodList'] && typeof detail['subGoodList'] === 'object') {
    const details = detail['subGoodList']
    for (let i = 0; i < details.length; i++) {
      let sub = details[i]
      if (subGoodId + '' !== sub.id + '') {
        continue
      }
      item.subGoodName = sub.subGoodName
      item.price = sub.price
      item.subGood = sub
      break
    }
  }
  item.subGoodId = subGoodId
  const pics = detail['goodPictureList']
  if (pics && typeof pics === 'object') {
    if (pics.length === 0) {
      item.image1 = product1
      item.image2 = product1
    }
    if (pics.length === 1) {
      item.image1 = pics[0].image
      item.image2 = pics[0].image
    }
    if (pics.length > 1) {
      item.image1 = pics[0].image
      item.image2 = pics[1].image
    }
  }
  return item
}

export const formatTime = (time) => {
  for (let i = 1; i < time.length; i++) {
    time[i] = time[i] < 10 ? '0' + time[i] : '' + time[i]
  }
  return time[0] + '-' + time[1] + '-' + time[2] + ' ' + time[3] + ':' + time[4] + ':' + time[5]
}
